package com.gulimall.product.service.impl;

import com.gulimall.product.entity.AttrEntity;
import com.gulimall.product.entity.AttrGroupEntity;
import com.gulimall.product.service.AttrGroupService;
import com.gulimall.product.service.AttrService;
import com.gulimall.product.vo.AttrGroupRelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.Query;

import com.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.gulimall.product.service.AttrAttrgroupRelationService;
import org.springframework.transaction.annotation.Transactional;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrGroupService attrGroupService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 解除分组和属性之间的关联关系，
     * 1、先根据分组id和属性id移除中间表数据（pms_attr_attrgroup_relation）
     * 2、其次修改属性表中（pms_attr）关联的相关字段（分组id和分组名称）赋 null值
     * @param vos
     */
    @Transactional
    @Override
    public void deleteAttrRelation(AttrGroupRelationVo[] vos) {
        //将传来的vos转换为AttrAttrgroupRelationEntity类型的list集合
        List<AttrAttrgroupRelationEntity> entities = Arrays.asList(vos).stream().map((item) -> {
            AttrAttrgroupRelationEntity attrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, attrgroupRelationEntity);
            return attrgroupRelationEntity;
        }).collect(Collectors.toList());

        //删除关联表中的数据
        this.baseMapper.deleteAttrRelation(entities);

        //将属性表所关联的属性分组修改为null
        List<AttrEntity> attrEntityList = entities.stream().map((item) -> {
            AttrEntity attrEntity = new AttrEntity();
            attrEntity.setAttrId(item.getAttrId());
            attrEntity.setAttrGroupId(null);
            attrEntity.setAttrGroupName(null);
            return attrEntity;
        }).collect(Collectors.toList());
        attrService.updateBatchById(attrEntityList);
    }

    /**
     * 属性分组功能中的关联属性接口
     * 1、先根据属性id将分组id和分组名称关联到属性表中（pms_attr）
     * 2、其次在属性分组的中间表中（pms_attr_attrgroup_relation）添加相应数据
     * @param vos
     */
    @Transactional
    @Override
    public void addAttrRelation(List<AttrGroupRelationVo> vos) {
        //1、先根据属性id将分组id和分组名称关联到属性表中（pms_attr）
        List<AttrEntity> attrEntityList = vos.stream().map((item) -> {
            AttrEntity attrEntity = new AttrEntity();
            attrEntity.setAttrId(item.getAttrId());
            attrEntity.setAttrGroupId(item.getAttrGroupId());
            //根据属性分组id获取属性分组的信息，提取属性分组名称
            AttrGroupEntity attrGroup = attrGroupService.getById(item.getAttrGroupId());
            attrEntity.setAttrGroupName(attrGroup.getAttrGroupName());
            return attrEntity;
        }).collect(Collectors.toList());
        attrService.updateBatchById(attrEntityList);

        //2、其次在属性分组的中间表中（pms_attr_attrgroup_relation）添加相应数据
        List<AttrAttrgroupRelationEntity> entities = vos.stream().map(item -> {
            AttrAttrgroupRelationEntity attrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, attrgroupRelationEntity);
            return attrgroupRelationEntity;
        }).collect(Collectors.toList());

        this.saveBatch(entities);
    }

}